package com.yk.template.spring.normal.service.user.impl;

import com.yk.template.spring.normal.dao.common.api.ICommonDao;
import com.yk.template.spring.normal.dao.user.api.IUserDao;
import com.yk.template.spring.normal.model.user.UserModel;
import com.yk.template.spring.normal.service.user.api.IUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

/**
 * Project Name:spring-normal
 * File Name:UserServiceImpl
 * Package Name:com.yk.template.spring.normal.service.user.impl
 * Date:2017/9/18 15:06
 * Author:zhangju
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */

@Service("userService")
public class UserServiceImpl implements IUserService{

    @Resource(name = "commonDao")
    private ICommonDao commonDao;

    @Resource(name = "userDao")
    private IUserDao userDao;

    protected final Log log= LogFactory.getLog(UserServiceImpl.class);

    /**
     * 查询所有用户注册信息
     *
     * @return 用户注册信息列表
     */
    @Override
    public List<UserModel> queryAll() {
        return this.userDao.selectAll();
    }

    /**
     * 根据 id 查询用户注册信息
     *
     * @param id 用户注册 id
     * @return 用户注册信息
     */
    @Override
    public UserModel queryById(String id) {

        UserModel model=new UserModel();
        model.setId(id);
        return this.userDao.select(model);
    }

    /**
     * 注册用户
     *
     * @param username 账户名
     * @param password 账户密码
     * @return 操作结果；true：成功，false：失败
     */
    @Override
    public boolean save(String username, String password) {

        UserModel model=new UserModel();
        model.setUserName(username);
        model.setUserPassword(password);
        model.setCreateDate(Timestamp.from(Instant.now()));
        model.setUpdateDate(Timestamp.from(Instant.now()));
        this.log.info(username+"\t注册账户");
        return this.commonDao.save(model);
    }

    /**
     * 更新用户
     *
     * @param id       用户 id
     * @param username 账户名
     * @param password 账户密码
     * @return 操作结果；true：成功，false：失败
     */
    @Override
    public boolean update(String id, String username, String password) {
        UserModel model=new UserModel();
        model.setId(id);
        model=this.userDao.select(model);
        model.setUserName(username);
        model.setUserPassword(password);
        model.setUpdateDate(Timestamp.from(Instant.now()));
        this.log.info(username+"\t更新账户");
        return this.commonDao.update(model);
    }

    /**
     * 注销用户
     *
     * @param id 用户 id
     * @return 操作结果；true：成功，false：失败
     */
    @Override
    public boolean delete(String id) {
        UserModel model=new UserModel();
        model.setId(id);
        model=this.userDao.select(model);
        this.log.info(model.getUserName()+"\t注销账户");
        return this.commonDao.delete(model);
    }
}
